<?php
    /**
     * 等待配货更新异常订单的发货单状态。
     * 
     * @author liugang<liugang@shopex.cn>
     * @version 1.0
     */
    //error_reporting(E_ALL ^ E_NOTICE);
    set_time_limit(0);

    require_once(dirname(__FILE__) . '/../../lib/init.php');

    $db = kernel::database();
    $sql = "SELECT w.delivery_cost_actual,w.branch_id,w.delivery_id as wms_delivery_id,w.delivery_time,o.delivery_bn,o.delivery_id,w.outer_delivery_bn,w.weight FROM sdb_wms_delivery as w left join sdb_ome_delivery as o on w.outer_delivery_bn=o.delivery_bn WHERE w.status='3' AND o.process='false' order by w.delivery_time DESC limit 0,1000";
    $deliverys = $db->select($sql);
    $deliveryObj = app::get('wms')->model('delivery');
    error_log(var_export($deliverys,1),3,DATA_DIR . '/updateome_list_log_' . date('Y-m-d') . '.log');

    $ApiLib=kernel::single('wms_event_trigger_delivery');
    $BranchLib=kernel::single('ome_branch');
    foreach ($deliverys as $key=> $delivery)
    {
        try
        {

            $wms_id = $BranchLib->getWmsIdById($delivery['branch_id']);
            $data = array(
                'delivery_bn' => $delivery['outer_delivery_bn'],
                'delivery_time' => $delivery['delivery_time'],
                'weight' => $delivery['weight'],
                'delivery_cost_actual' => $delivery['delivery_cost_actual'],
                'status'=>'delivery'
                );

            $res=kernel::single('erpapi_router_response')->set_channel_id($wms_id)->set_api_name('wms.delivery.status_update')->dispatch($data);
            error_log(var_export(array($data,$res,$wms_id),1),3,DATA_DIR . '/updateome_log_' . date('Y-m-d') . '.log');

            //发货回传前端店铺-begin
            $dlyObj = app::get('wms')->model('delivery');
            $Oshop = app::get('ome')->model('shop');
            $deliveryBillLib = kernel::single('wms_delivery_bill');

            $delivery_id = $delivery['wms_delivery_id'];
            $dly = $dlyObj->dump(array('delivery_id' => $delivery_id),'*',array('delivery_items'=>array('*')));

            $shop = $Oshop->dump(array('shop_id' => $dly['shop_id']),'isWD,shop_bn,is_sendshop');

            $logi_no = $deliveryBillLib->getPrimaryLogiNoById($delivery_id);

            if($shop['isWD'] == 'true' && $shop['is_sendshop'] == 'true')
            {
                //调用回传物流信息的接口
                $delivery_bn = $dly['delivery_bn'];
                $Oome_delivery = app::get('ome')->model('delivery');
                $Oome_delivery_order = app::get('ome')->model('delivery_order');
                $Oome_orders = app::get('ome')->model('orders');
                $ome_delivery = $Oome_delivery->dump(array('delivery_bn' => $dly['outer_delivery_bn']),'delivery_id');
                $ome_delivery_order = $Oome_delivery_order->dump(array('delivery_id' => $ome_delivery['delivery_id']),'order_id');
                $ome_orders = $Oome_orders->dump(array('order_id' => $ome_delivery_order['order_id']),'relate_order_bn');
                $order_bn = $ome_orders['relate_order_bn'];
                $logi_no = $logi_no;
                $logi_name = $dly['logi_name'];
                $shop_bn = $shop['shop_bn'];
                kernel::single('ome_UpdateAXStatus')->UpdateAXStatus($shop_bn,$delivery_bn,$order_bn,$logi_no,$logi_name);
                $dlyObj->db->exec("update sdb_wms_delivery set is_sync_shop = true where  delivery_id ='" . $dly['delivery_id'] . "'");
            }elseif($shop['isWD'] == 'false' && $shop['is_sendshop'] == 'true'){
                $Oome_delivery = app::get('ome')->model('delivery');
                $ome_delivery = $Oome_delivery->dump(array('delivery_bn' => $dly['outer_delivery_bn']),'delivery_id');
                $Oome_delivery->call_delivery_api($ome_delivery['delivery_id'],false);
                $dlyObj->db->exec("update sdb_wms_delivery set is_sync_shop = true where  delivery_id ='" . $dly['delivery_id'] . "'");
            }
            //发货回传前端店铺-end



        } catch (Exception $e) {
            echo $e->getMessage();
        }

    }

    ?>